from google.appengine.ext import db

from datetime import *

from Activity import *
from Location import *
from util import *

class CalendarEntry(db.Model):
    userId = db.IntegerProperty()
    activity = db.ReferenceProperty(Activity)
    duration = db.FloatProperty() # in hours
    location = db.ReferenceProperty(Location)
    date = db.DateTimeProperty()

class Calendar():
    def __init__(self, userId):
        self.userId = userId

    def addTZInfo(self, entries):
        for entry in entries:
            if entry.date.tzinfo == None:
                entry.date = addUTC(entry.date)
            entry.date = fromUTC(entry.date)

    def getEntryAtTime(self, date):
        calEntryQ = db.GqlQuery("SELECT * FROM CalendarEntry WHERE userId=:1 AND date < :2 ORDER BY date DESC", self.userId, date)
        calEntry = calEntryQ.get()
        if not calEntry:
            return None
        self.addTZInfo([calEntry])
        if (date - calEntry.date) > timedelta(hours=calEntry.duration):
            return None
        return calEntry

    def getActivityAtTime(self, date):
        calEntry = self.getEntryAtTime(date)
        if calEntry:
            return calEntry.activity
        return None

    def getEntriesAtDate(self, date):
        d1 = localDatetime(date.year, date.month, date.day, 0, 0)
        d2 = d1 + timedelta(hours=24)
        return self.getEntriesInInterval(d1, d2)

    def getEntriesInInterval(self, date1, date2, maxEntries=200):
        calEntryQ = db.GqlQuery("SELECT * FROM CalendarEntry WHERE userId=:1 AND date >= :2 AND date < :3 ORDER BY date ASC", self.userId, date1, date2)
        entries = calEntryQ.fetch(maxEntries)
        self.addTZInfo(entries)
        return entries

    def deleteAllEntriesOnDay(self, date):
        d1 = localDatetime(date.year, date.month, date.day, 0, 0)
        d2 = localDatetime(date.year, date.month, date.day, 0, 0) + timedelta(hours=24)
        self.deleteEntriesInInterval(d1, d2)
        
    def deleteEntriesInInterval(self, date1, date2, maxEntries=200):
        calEntryQ = db.GqlQuery("SELECT __key__ FROM CalendarEntry WHERE userId=:1 AND date >= :2 AND date < :3 ORDER BY date ASC", self.userId, date1, date2)
        #print calEntryQ.fetch(maxEntries)
        db.delete(calEntryQ.fetch(maxEntries))

    def addEntry(self, activity, date, duration, location):
        calEntry = CalendarEntry()
        calEntry.userId = self.userId
        calEntry.date = date
        calEntry.activity = activity
        calEntry.duration = float(duration)
        calEntry.location = location
        #print "Added ", activity.name, location
        calEntry.put()

    def getPrevAndNextLocation(self, entry):
        d1 = entry.date - timedelta(minutes=1)
        e1 = self.getEntryAtTime(d1)
        if e1 == None:
            l1 = None
        else:
            l1 = e1.location
        d2 = entry.date + timedelta(hours=entry.duration) + timedelta(minutes=1)
        e2 = self.getEntryAtTime(d2)
        if e2 == None:
            l2 = None
        else:
            l2 = e2.location
        return l1, l2

